﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Reversi
{
    public partial class Game : System.Web.UI.Page
    {
        //Board Draw Information
        private static readonly int BOARD_SIZE = Board.BOARD_SIZE;
        private static readonly int BOARD_CELL = 40;
        private static readonly Color COLOR_WHITE = Color.White;
        private static readonly Color COLOR_BLACK = Color.Black;
        private static readonly Color COLOR_EMPTY = Color.FromArgb(190, 187, 187);
        private static readonly Color COLOR_BACKGROUND = Color.Yellow;

        //Board Session Names
        private static readonly string TABLE = "Table";

        //User Cookie Names
        private static readonly string SESSION_ID = "SessionID";

        //Move Information
        private int MoveCol;
        private int MoveRow;

        protected void Page_Load(object sender, EventArgs e)
        {
            string sessionid = Session[SESSION_ID] as string;
            if (sessionid == null || sessionid == "")
            {
                Response.Redirect("Login.aspx");
            }

            if (!Page.IsPostBack)
            {
                CreateTable();
                Console.WriteLine("calling board load with seesionid:" + Session[SESSION_ID] as string);
                UpdateTable(DataAccessLayer.BoardLoad(Session[SESSION_ID] as string));
            }
        }

        private void CreateTable()
        {
            Table table = new Table();
            TableRow row;
            TableCell cell;

            for (int i = 0; i < BOARD_SIZE; i++)
            {
                row = new TableRow();
                for (int j = 0; j < BOARD_SIZE; j++)
                {
                    cell = new TableCell();
                    row.Controls.Add(cell);

                    cell.Height = BOARD_CELL;
                    cell.Width = BOARD_CELL;
                    cell.ID = (i.ToString() + "," + j.ToString());
                    cell.BackColor = COLOR_EMPTY;
                    cell.Attributes.Add("onclick", "Clicky('" + cell.ID.ToString() + "')");
                    cell.Attributes.Add("style", "cursor:pointer");
                }
                table.Controls.Add(row);
            }

            UpdateHolder.Controls.Add(table);

            Session[TABLE] = table;
        }

        private void UpdateTable(Board board)
        {
            Table temp = Session[TABLE] as Table;

            if (temp != null)
            {
                for (int i = 0; i < BOARD_SIZE; i++)
                {
                    for (int j = 0; j < BOARD_SIZE; j++)
                    {
                        if (board.GetSquareContents(i, j) == Board.Black)
                        {
                            temp.Rows[i].Cells[j].BackColor = COLOR_BLACK;
                        }
                        else if (board.GetSquareContents(i, j) == Board.White)
                        {
                            temp.Rows[i].Cells[j].BackColor = COLOR_WHITE;
                        }
                        else if (board.GetSquareContents(i, j) == Board.Empty)
                        {
                            temp.Rows[i].Cells[j].BackColor = COLOR_EMPTY;
                        }
                    }
                }
                Session[TABLE] = temp;


                UpdateHolder.Controls.Clear();
                UpdateHolder.Controls.Add(temp);
            }
        }


        private void GetClicked()
        {
            string[] XY = MoveID.Value.Split(',');

            if (XY.Length >= 0 && XY.Length <= 2)
            {
                MoveRow = int.Parse(XY[0]);
                MoveCol = int.Parse(XY[1]);
            }
        }

        private void MakeMove()
        {
            GetClicked();

            string sessionid = Session[SESSION_ID] as string;
            int? gameid = DataAccessLayer.GetGameID(sessionid);
            int userid = DataAccessLayer.getUserFromSession(sessionid).id;
            int whoturn = DataAccessLayer.getWhoseTurn(gameid.Value);

            Board board = DataAccessLayer.BoardLoad(sessionid);

            if (gameid != null && 
                whoturn == userid &&
                board.IsValidMove(Board.White, MoveRow, MoveCol))
            {
                board.MakeMove(Board.White, MoveRow, MoveCol);
            }
            else if (gameid != null &&
                     whoturn == userid &&
                     board.IsValidMove(Board.Black, MoveRow, MoveCol))
            {
                board.MakeMove(Board.Black, MoveRow, MoveCol);
            }
            else
            {
                return;
            }

            DataAccessLayer.BoardSave(Session[SESSION_ID] as string, board);
            UpdateTable(board);
        }

        private void CheckWin()
        {
            //Board board = DataAccessLayer.BoardLoad(Session[SESSION_ID] as string);

            //if (!board.hasanyvalidmove(Color.White))
            //{

            //}
            //else if (!board.hasanyvalidmove(board.black))
            //{

            //}
        }

        protected void MadeMove_Click(object sender, EventArgs e)
        {
            if (DataAccessLayer.IsTurn(Session[SESSION_ID] as string))
            {
                MakeMove();
                CheckWin();

                int? gameid = DataAccessLayer.GetGameID(Session[SESSION_ID] as string);

                if (gameid != null && DataAccessLayer.getWhoseTurn(gameid.Value) == Board.White)
                {
                    lblTurn.Text = "Turn: White";
                }
                else if (gameid != null && DataAccessLayer.getWhoseTurn(gameid.Value) == Board.Black)
                {
                    lblTurn.Text = "Turn: Black";
                }
            }
        }

        protected void Timer_Refresh_Tick(object sender, EventArgs e)
        {
           UpdateTable(DataAccessLayer.BoardLoad(Session[SESSION_ID] as string));
        }
    }
}